package param;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JTextField;

import logger.Logger;
import Modele.Parseur;
import vue.Fenetre;
import xml.Commande;
import xml.CommandeProvider;
import xml.Importance;
import xml.ImportanceProvider;

public class EcouteurParametre implements ActionListener, WindowListener {

	public static final EcouteurParametre INSTANCE = new EcouteurParametre();
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("couleur"))
		{		
			Color c = JColorChooser.showDialog(null, "Couleur", Color.white);
			((JButton)e.getSource()).setBackground(c);
		}
		else if(e.getActionCommand().equals("save"))
		{
			for(int i=0;i<FenetreParametre.OPENED_FRAME.cmdLabels.length;i++)
			{
				Commande cmd = CommandeProvider.INSTANCE.getCommande(FenetreParametre.OPENED_FRAME.cmdLabels[i].getText(), true);
				cmd.equiv = FenetreParametre.OPENED_FRAME.cmdEquivs[i].getText();
				cmd.display = FenetreParametre.OPENED_FRAME.cmdVisibles[i].isSelected();
				try
				{
				cmd.importance = ImportanceProvider.INSTANCE.getImportance(Integer.parseInt(FenetreParametre.OPENED_FRAME.cmdImportances[i].getText()));
				}
				catch(Exception ex)
				{
					ex.printStackTrace();
					Logger.INSTANCE.logError(ex);
				}
			}
			
			for(int i=0;i<FenetreParametre.OPENED_FRAME.impLabels.length;i++)
			{
				int impId = Integer.parseInt(FenetreParametre.OPENED_FRAME.impLabels[i].getText());
				boolean used = false;
				for(Commande cm : CommandeProvider.INSTANCE.getCommandes())
				{
					if(cm.importance.importance == impId)
					{
						used = true;
						break;
					}
				}
				
				if(!used)
				{
					ImportanceProvider.INSTANCE.removeImportance(impId);
				}
				else
				{
					Importance imp = ImportanceProvider.INSTANCE.getImportance(impId);
					imp.color = FenetreParametre.OPENED_FRAME.impButtons[i].getBackground();
				}
			}
			
			CommandeProvider.INSTANCE.saveAll();
			ImportanceProvider.INSTANCE.saveAll();
			Parseur.INSTANCE.refreshEquiv();
			
			//Fenetre.INSTANCE.liste_logs.repaint();
			Fenetre.INSTANCE.liste_log_contenu.fireChange();
			
			FenetreParametre.OPENED_FRAME.dispose();
			//FenetreParametre.OPENED_FRAME.setVisible(false);
			//FenetreParametre.OPENED_FRAME = null;
		}
	}

	@Override
	public void windowActivated(WindowEvent e) {}

	@Override
	public void windowClosed(WindowEvent e) {
		FenetreParametre.OPENED_FRAME = null;
		System.out.println("Close");
	}

	@Override
	public void windowClosing(WindowEvent e) {}

	@Override
	public void windowDeactivated(WindowEvent e) {}

	@Override
	public void windowDeiconified(WindowEvent e) {}

	@Override
	public void windowIconified(WindowEvent e) {}

	@Override
	public void windowOpened(WindowEvent e) {}


	
}
